package com.behome.netty.netty_B.bean;


import cn.hutool.json.JSONObject;
import com.behome.netty.common.ByteConventerKit;
import com.behome.netty.netty_B.enums.ReBackAnnotation;
import com.behome.netty.netty_B.enums.ReBackCmdType;

import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;


@ReBackAnnotation(
        type = ReBackCmdType.TASK_SWITCH
)
public class TerminalTaskSwitch extends BusinessReBackBase {
    private byte flag;
    private byte taskType;
    private String ebmId;
    private Date time;

    public TerminalTaskSwitch() {
    }

    public byte getTaskType() {
        return this.taskType;
    }

    public void setTaskType(byte taskType) {
        this.taskType = taskType;
    }

    public String getEbmId() {
        return this.ebmId;
    }

    public void setEbmId(String ebmId) {
        this.ebmId = ebmId;
    }

    public byte getFlag() {
        return this.flag;
    }

    public void setFlag(byte flag) {
        this.flag = flag;
    }

    public Date getTime() {
        return this.time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    @Override
    public byte[] serialize() {
        return null;
    }

    @Override
    public BusinessReBackBase deserialize(byte[] businessData) {
        if (businessData != null && businessData.length == 24) {
            ByteBuffer byteBuffer = ByteBuffer.wrap(businessData);
            this.setFlag(byteBuffer.get());
            this.setTaskType(byteBuffer.get());
            byte[] ebmIdData = new byte[18];
            byteBuffer.get(ebmIdData);
            String ebmId = ByteConventerKit.deserializeEbmId(ebmIdData);
            this.setEbmId(ebmId);
            byte[] timeData = new byte[4];
            byteBuffer.get(timeData);
            this.setTime(ByteConventerKit.bytesToDatetime(timeData));
        }
        return this;
    }

    @Override
    public boolean validateBeforeSerialize() {
        return true;
    }

    @Override
    public String toCHJsonString() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOnce("切换标识", this.flag == 1 ? "任务开始" : "任务结束");
        jsonObject.putOnce("任务类型", this.taskType);
        jsonObject.putOnce("广播编号", this.ebmId);
        jsonObject.putOnce("发生时间", simpleDateFormat.format(this.time));
        return jsonObject.toString();
    }
}
